package com.fly.system.service.impl;

import com.fly.common.model.BaseModel;
import com.fly.common.model.SysUserModel;
import com.fly.common.model.ZtreeModel;
import com.fly.common.util.UserlUtil;
import com.fly.system.mapper.SysMenuMapper;
import com.fly.system.model.SysMenuModel;
import com.fly.system.service.SysMenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @CLASSNAME: SysMenuServiceImpl
 * @author: Fly
 * 日期: 2020/6/13  13:17
 * 描述:menu菜单
 **/

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenuModel> findMenuListByUserId(SysUserModel mdoel) {
        if(mdoel.isAdmin()){
            return sysMenuMapper.selectAdminMenu();
        } else {
            return sysMenuMapper.selectMenuListByUserId(mdoel.getId());
        }
    }

    @Override
    public List<ZtreeModel> findMenuTree() {
        List<ZtreeModel> tree = sysMenuMapper.selectMenuTree();
        ZtreeModel model = new ZtreeModel();
        model.setId(0L);
        model.setTitle("FLY-ADMIN");
        tree.add(model);
        return tree;
    }

    @Override
    public int addSave(SysMenuModel model) {
        UserlUtil.setInsertColumn(model);
        return sysMenuMapper.insert(model);
    }

    @Override
    public List<SysMenuModel> findPage(SysMenuModel model) {
        List<SysMenuModel> list = sysMenuMapper.selectList(model);
        return list;
    }

    @Override
    public SysMenuModel findMenuModel(SysMenuModel model) {
        SysMenuModel menu = sysMenuMapper.selectOne(model);
        menu.setParentText(sysMenuMapper.selectByPrimaryKey(menu.getId()).getMenuName());
        return menu;
    }

    @Override
    public SysMenuModel findMenuModelById(Long id) {
        SysMenuModel model = new SysMenuModel();
        model.setId(id);
        SysMenuModel menu = sysMenuMapper.selectOne(model);
        menu.setParentText(sysMenuMapper.selectByPrimaryKey(menu.getId()).getMenuName());
        return menu;
    }

    @Override
    public int deleteModel(SysMenuModel model) {
        int i = sysMenuMapper.deleteByPrimaryKey(model.getId());
        return i;
    }

    @Override
    public int editModelById(SysMenuModel model) {
        UserlUtil.setUpdateColumn(model);
        int i = sysMenuMapper.updateByPrimaryKeySelective(model);
        return i;
    }

}
